package com.example.practice03.service;

import com.example.practice03.bean.RegionStats;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVRecord;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.io.Resource;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.io.*;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

@Service
public class DataService implements InitializingBean {
    private List<RegionStats> list = new ArrayList<>();

    @Scheduled(cron = "0 0 7 * * *")
    public void getRegionStats() throws IOException {
        String url = "https://gitee.com/dgut-sai/COVID-19/raw/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv";
        //爬取csv文件
        RequestEntity<Void> requestEntity = RequestEntity
                .get(url)
                .headers(httpHeaders -> httpHeaders.add("User-Agent","Mozilla/5.8"))
                .build();
        ResponseEntity<Resource>  exchange = new RestTemplate().exchange(requestEntity,Resource.class);
        Resource body = exchange.getBody();
        System.out.println(body);
        //通过body获取数据
        InputStream inputStream = body.getInputStream();
        Reader reader = new InputStreamReader(inputStream);

        //解析数据
        Iterable<CSVRecord> records = CSVFormat.RFC4180.withFirstRecordAsHeader().parse(reader);
        for(CSVRecord record : records){
            RegionStats regionStats = new RegionStats();
            regionStats.setState(record.get("Province/State"));
            regionStats.setCountry(record.get("Country/Region"));
            regionStats.setLatestTotalCases(Integer.parseInt(record.get(record.size()-1)));
            regionStats.setDiffFromPrevDay(Integer.parseInt(record.get(record.size()-1))-Integer.parseInt(record.get(record.size()-2)));
            list.add(regionStats);
            System.out.println(regionStats);
        }
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        //程序启动的时候获取数据
        getRegionStats();
    }

    public List<RegionStats> getAllList(){
        return list;
    }

    public List<RegionStats> getListByState(String state){
        List<RegionStats> result = new ArrayList<>();
        for (RegionStats regionStats : list){
            if(regionStats.getState().equals(state)){
                result.add(regionStats);
            }
        }
        return result;
    }

    public List<RegionStats> getListByCountry(String country){
        List<RegionStats> result = new ArrayList<>();
        for (RegionStats regionStats : list){
            if(regionStats.getCountry().equals(country)){
                result.add(regionStats);
            }
        }
        return result;
    }
}
